package com.ilink.groundservice.controller.AiX;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ilink.groundservice.entity.VO.AiX.AiXActivityResponse;
import com.ilink.groundservice.entity.VO.AiX.AiXProjectResponse;
import com.ilink.groundservice.service.AiX.AiXProjectService;
import com.ilink.ilinkapi.client.UserServiceClient;
import com.ilink.ilinkapi.dto.ground.AiX.*;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.domain.User;
import com.ilink.ilinkcommon.log.method.MethodLog;
import com.ilink.ilinkcommon.utils.BeanUtils;
import com.ilink.ilinkcommon.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RequestMapping("/ground/aix/project")
@RestController
@RequiredArgsConstructor
public class AiXProjectController {
    private final AiXProjectService aiXProjectService;
    private final UserServiceClient userServiceClient;

    @MethodLog("分页返回活动元数据")
    @GetMapping("/get_projects")
    public R getProjects(AiXAreaFilterRequest request) {
        try {
            IPage<AiXProjectResponse> responseIPage = aiXProjectService.getProjects(request);
            if (responseIPage != null) {
                return R.ok("分页返回活动元数据", responseIPage);
            } else {
                return R.error("查询活动数据失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    @MethodLog("插入活动元数据")
    @PostMapping("/insert_project")
    public R insertProject(AiXProjectInsertRequest request) {
        if (!isTeacher(UserContext.getUserId())) {
            return R.error("无教师权限");
        }
        try {
            aiXProjectService.insertProject(request);
            return R.ok("活动插入成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    @MethodLog("更新项目数据")
    @PostMapping("/update_project")
    public R updateProject(@RequestBody AiXProjectUpdateRequest request) {
        if (!isTeacher(UserContext.getUserId())) {
            return R.error("无教师权限");
        }
        try {
            aiXProjectService.updateProject(request);
            return R.ok("项目更新成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }
    @MethodLog("更新项目封面图")
    @PostMapping("/update_project_cover")
    public R updateProjectCover(MultipartFile file, Long projectId, Long areaId ) {
        if (!isTeacher(UserContext.getUserId())) {
            return R.error("无教师权限");
        }
        try {
            aiXProjectService.updateProjectCover(file, projectId, areaId);
            return R.ok("项目封面图更新成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    @MethodLog("删除项目元数据")
    @PostMapping("/delete_project/{projectId}")
    public R deleteActivity(@PathVariable Long projectId) {
        if (!isTeacher(UserContext.getUserId())) {
            return R.error("无教师权限");
        }
        try {
            aiXProjectService.deleteProject(projectId);
            return R.ok("项目删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    @MethodLog("获取单个项目")
    @GetMapping("/get_project/{projectId}")
    public R getProject(@PathVariable Long projectId) {
        try {
            AiXProjectResponse response = aiXProjectService.getProject(projectId);
            return R.ok("项目获取成功", response);
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    @MethodLog("获取项目最常用的8个标签标签")
    @GetMapping("/get_project_tags")
    public R getProjectTags(@RequestParam Long areaId) {
        try {
            List<String> tags = aiXProjectService.getProjectTags(areaId);
            return R.ok("项目最常用的8个标签", tags);
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }
    // 判断用户是否有教师权限
    private boolean isTeacher(String userId) {
        R whoami = userServiceClient.whoami(userId);
        HttpStatus status = HttpStatus.valueOf(whoami.getCode());
        if (!status.is2xxSuccessful()) {
            log.error(String.format("获取用户信息失败，用户ID: %s", userId));
            return false;
        }
        User userDetail = BeanUtils.copyBean(whoami.getData(), User.class);
        String userType = userDetail.getUserType();
        return Arrays.asList("BIAdmin", "L0101", "L0107", "L0108", "L0109").contains(userType);
    }
}
